﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Pavilion.GameTools;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using Pavilion.DesignerTool.ViewTools.ConnectableListBox;
using GalaSoft.MvvmLight.Command;
using Pavilion.DesignerTool.NodeDesignerTool.Scripting;
using System.Xml.Linq;
using System.IO;
using Pavilion.DesignerTool.ViewTools;

namespace Pavilion.DesignerTool
{
    public class NodeDesignerToolViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public AnimationResourceData AnimationResourceData { get; set; }
        public bool CanStepOut { get { return (CurrentContainerNode != null) && (CurrentContainerNode.ParentContainer != null); } }
        public ContainerNode CurrentContainerNode { get { return (ConnectableItemContainer != null) ? (ContainerNode)ConnectableItemContainer.Data : null; } }
        public NodeLayoutSerializer nodeLayoutSerializer { get; set; }
        public ContainerNodeWrapper ConnectableItemContainer { get { return connectableItemContainer; } set { connectableItemContainer = value; OnPropertyChanged("ConnectableItemContainer"); }  }

        private ContainerNodeWrapper connectableItemContainer;

        public NodeDesignerToolViewModel()
        {
            nodeLayoutSerializer = new NodeLayoutSerializer();
        }

        internal void LoadResourceItem(AnimationResourceData animationResourceData)
        {
            this.AnimationResourceData = animationResourceData;
            ConnectableItemContainer = AnimationResourceData.RootContainerNodeWrapper;
            this.nodeLayoutSerializer.DeserializeCollection(AnimationResourceData.RootContainerNodeWrapper, GameEngine.NodeLayoutDirectory + @AnimationResourceData.ResourceName + @"\NodeScript.xml");
        }
        
        internal void StepIntoNode(IConnectableItem connectableItem)
        {
            if (connectableItem is IConnectableItemContainer)
                this.ConnectableItemContainer = connectableItem as ContainerNodeWrapper;
        }

        internal void StepOutOfNode()
        {
            this.ConnectableItemContainer = ConnectableItemContainer.ParentContainerNodeWrapper;
        }

        internal void AddNewAnimationNode()
        {
            this.ConnectableItemContainer.AddNode(new AnimationNode(this.CurrentContainerNode.GetUniqueName("AnimationNode")));
        }

        internal void AddNewContainerNode()
        {
            this.ConnectableItemContainer.AddNode(new ContainerNode(this.CurrentContainerNode.GetUniqueName("ContainerNode")));
        }

        internal void RemoveNode(NodeBaseWrapper node)
        {
            this.ConnectableItemContainer.RemoveNode(node);
        }

        internal void AddLink(NodeBaseWrapper startNode, NodeBaseWrapper endNode)
        {
            ConnectableItemContainer.AddLink(startNode, endNode);
        }

        internal void RemoveLink(LinkWrapper linkWrapper)
        {
            linkWrapper.Link.Dispose();
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        public void SaveNodeLayoput()
        {
            MessageBoxResult result = MessageBox.Show("About save all modified Layout Data. Proceed?", "Are you sure about this?!", MessageBoxButton.YesNo);

            if (result == MessageBoxResult.Yes)
            {
                XDocument nodeLayoutDocument = nodeLayoutSerializer.SerializeCollection(AnimationResourceData.RootContainerNodeWrapper);
                if (!Directory.Exists(GameEngine.NodeLayoutDirectory + @AnimationResourceData.ResourceName))
                    CreateDirectiory(GameEngine.NodeLayoutDirectory + @AnimationResourceData.ResourceName);

                nodeLayoutDocument.Save(GameEngine.NodeLayoutDirectory + @AnimationResourceData.ResourceName + "/" + "NodeScript.xml");
                MessageBox.Show("Modified Layout data saved!!!");
            }
        }

        private void CreateDirectiory(string directioryToCreate)
        {
            Directory.CreateDirectory(directioryToCreate);
        }
    }
}
